package com.tjcd.project.library_base.common.upload.model;


import com.tjcd.project.library_base.base.bean.BaseHttpBean;
import com.tjcd.project.library_base.common.upload.OssUpLoadSignService;
import com.tjcd.project.library_base.common.upload.bean.UploadBean;
import com.tjcd.project.library_base.common.upload.contract.FileUploadContract;
import com.tjcd.project.library_base.common.upload.manager.FileNetManager;
import com.tjcd.project.library_base.common.upload.net.BaseFileObserver;
import com.tjcd.project.library_base.common.upload.net.BaseModel;
import com.tjcd.project.library_base.common.upload.net.BeanNetUnit;
import com.tjcd.project.library_base.common.upload.progress.FileUploadObserver;
import com.tjcd.project.library_base.common.upload.progress.UploadFileRequestBody;

import java.io.File;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;

public class FileUploadModel extends BaseModel implements FileUploadContract.Model {
    /**
     * 文件上传
     */
    BeanNetUnit mFileUploadNet;

    @Override
    public void fileUploadOss(File file, String type, BaseFileObserver observer) {
        mFileUploadNet = new BeanNetUnit()
                .setObservable(FileNetManager.getRetrofit().create(OssUpLoadSignService.class).getOssUpLoadSign(type))
                .request(observer);
    }

    @Override
    public void fileUpload(File file, String type, BaseFileObserver observer) {
        RequestBody uploadType = RequestBody.create(MediaType.parse("multipart/form-data"), type);
        RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part myBody = MultipartBody.Part.createFormData("file", file.getName(), fileBody);
        mFileUploadNet = new BeanNetUnit()
                .setObservable(FileNetManager.getBaseFileRetrofit().create(FileUploadService.class).fileUpload(uploadType, myBody))
                .request(observer);
    }

    @Override
    public void fileUploadProgress(File file, String type, FileUploadObserver inter, BaseFileObserver observer) {
        RequestBody uploadType = RequestBody.create(MediaType.parse("multipart/form-data"), type);
        UploadFileRequestBody uploadFileRequestBody = new UploadFileRequestBody(file, inter);
        MultipartBody.Part myBody = MultipartBody.Part.createFormData("file", file.getName(), uploadFileRequestBody);
        mFileUploadNet = new BeanNetUnit()
                .setObservable(FileNetManager.getBaseFileRetrofit().create(FileUploadService.class).fileUpload(uploadType, myBody))
                .request(observer);
        try { // 进度回调
            mFileUploadNet.getObservable().subscribe(inter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface FileUploadService {
        @Multipart
        @POST("filesystem/oss/fileUpload")
        Observable<BaseHttpBean<UploadBean>> fileUpload(@Part("uploadType") RequestBody uploadType, @Part MultipartBody.Part file);
    }

    @Override
    public void cancelBiz() {
        cancelRequest(mFileUploadNet);
    }
}
